{% extends "admin/layout.html" %}
{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col">
            <h2>审批步骤管理 - {{ flow.name }}</h2>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <form method="POST" action="{{ url_for('jira.manage_approval_steps', flow_id=flow.id) }}">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

                        <div id="steps-container">
                            {% for step in flow.steps|sort(attribute='order') %}
                            <div class="step-item mb-4 p-3 border rounded">
                                <h5>步骤 {{ step.order + 1 }}</h5>
                                <div class="mb-3">
                                    <label class="form-label">审批人</label>
                                    <select class="form-select" name="user_id_{{ step.id }}">
                                        {% for user in users %}
                                        <option value="{{ user.id }}" {% if step.user_id == user.id %}selected{% endif %}>
                                            {{ user.name }}
                                        </option>
                                        {% endfor %}
                                    </select>
                                </div>
                                <div class="mb-3 form-check">
                                    <input type="checkbox" class="form-check-input" name="require_comment_{{ step.id }}"
                                           {% if step.require_comment %}checked{% endif %}>
                                    <label class="form-check-label">必须填写审批意见</label>
                                </div>
                            </div>
                            {% endfor %}
                        </div>

                        <div class="d-flex justify-content-between mt-3">
                            <button type="button" class="btn btn-outline-primary" id="add-step">
                                <i class="fas fa-plus"></i> 添加步骤
                            </button>
                            <div>
                                <button type="submit" class="btn btn-primary">保存</button>
                                <a href="{{ url_for('jira.approval_flow_list') }}" class="btn btn-secondary">返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    let stepCount = {{ flow.steps|length }};

    $('#add-step').click(function() {
        stepCount++;
        const stepHtml = `
        <div class="step-item mb-4 p-3 border rounded">
            <h5>步骤 ${stepCount}</h5>
            <div class="mb-3">
                <label class="form-label">审批人</label>
                <select class="form-select" name="new_user_id_${stepCount}">
                    {% for user in users %}
                    <option value="{{ user.id }}">{{ user.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="mb-3 form-check">
                <input type="checkbox" class="form-check-input" name="new_require_comment_${stepCount}">
                <label class="form-check-label">必须填写审批意见</label>
            </div>
        </div>
        `;
        $('#steps-container').append(stepHtml);
    });
});
</script>
{% endblock %}